package br.net.woodstock.tickettoride.web.seam.security;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

import br.net.woodstock.tickettoride.api.access.UserService;
import br.net.woodstock.tickettoride.orm.User;
import br.net.woodstock.tickettoride.orm.UserStatus;
import br.net.woodstock.tickettoride.web.jsf.security.common.JSFSecurityUtils;

@Name("authenticator")
public class Authenticator {

	@In
	private Credentials	credentials;

	@In
	private Identity	identity;

	@In(value = "#{userService}")
	private UserService	userService;

	public boolean authenticate() {
		User user = this.userService.getByLoginPassword(this.credentials.getUsername(), this.credentials.getPassword());
		if ((user != null) && (user.getStatus() == UserStatus.ACTIVE)) {
			this.identity.addRole(user.getType().name());
			JSFSecurityUtils.setSessionUser(user);
			return true;
		}
		return false;
	}

}
